<?php
	header("Content-type: application/x-javascript");
	use\ampleharvest\permissions;

	require_once dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))) . '/config/cConfig.php';
	
	$configx = new \ampleharvest\cConfig();
	
	require_once $configx->C_STR_AMPLEHARVEST_CODE . "/classes/User_Manager/cUserValidation.php";
	require_once $configx->C_STR_AMPLEHARVEST_CODE . "/config/cPermissions.php";
	require_once $configx->C_STR_AMPLEHARVEST_CODE . "/classes/search/solr/cDoc.php";

	$obj_UserValidation = new cUserValidation();
	$obj_UserValidation->require_Login(true);

	if (!$obj_UserValidation->check_Permission(permissions\cPermissions::C_STR_PERMISION_STATISTICS_VIEW))
	{
		die("You do not have permision to view this page");
	}
?>
Ext.define('AmpleHarvestPHP.objects.search.querypanel.form.cGroundEffect',
{
	extend				: 'Ext.form.Panel',
	stateful			: false,
	m_arr_StateEvents	: [],

	height				: 600,
	requires			:
	[
			'Ext.form.field.Number', 
			'Ext.form.field.Text',
			'Ext.form.FieldSet', 
			'Ext.form.field.Date',
			'AmpleHarvest.addOns.keyboard.cPlugin',
			'AmpleHarvest.addOns.keyboard.layout.Cyrillic_RU',
			'AmpleHarvest.elements.combobox.cFacet',
			'AmpleHarvest.objects.search.querypanel.form.AbstractSearchForm'
	],

	statics				:
	{
		C_STR_FIELD_COST		: "value.cost",
		C_STR_FIELD_CUSTOMER	: "string.customer",
		C_STR_FIELD_ORDERMETHOD	: "string.order_method",
		C_STR_FIELD_PUBLISHDATE	: "<?php print(cDoc::C_STR_FIELD_PUBLISH_DATE); ?>",
		C_STR_FIELD_HARVESTDATE	: "<?php print(cDoc::C_STR_FIELD_HARVEST_DATE); ?>",
		C_STR_FIELD_INDEXDATE	: "<?php print(cDoc::C_STR_FIELD_INDEX_DATE); ?>",
		C_STR_FIELD_SOURCE		: "<?php print(cDoc::C_STR_FIELD_SOURCE); ?>",
		C_STR_FIELD_TAXID		: "string.tax_payer_id"
	},

	constructor			: function()
	{
		this.PublishDateBox = Ext.create("Ext.form.FieldSet",
				{
					title		: "Publish Date",
					collapsible	: true,
					labelWidth	: 70,
					boxMinWidth	: 275,
					stateId		: 'publish_date',
					stateful	: false,
					listeners	:
					{
						resize	:
						{
							fn	: function()
							{
								this.up('form').save_State();
							}
						}
					}

				});

		this.PublishDateBox.add(Ext.create("Ext.form.field.Date",
		{
			fieldLabel	: "Start Date",
			dataType	: AmpleHarvest.objects.search.querypanel.form.AbstractSearchForm.C_STR_DATATYPE_DATE,
			anchor		: "100%",
			name		: this.self.C_STR_FIELD_PUBLISHDATE + "_start",
			allowBlank	: true
		}
		));

		this.PublishDateBox.add(Ext.create("Ext.form.field.Date",
		{
			fieldLabel	: "End Date",
			dataType	: AmpleHarvest.objects.search.querypanel.form.AbstractSearchForm.C_STR_DATATYPE_DATE,
			anchor		: "100%",
			name		: this.self.C_STR_FIELD_PUBLISHDATE + "_end",
			allowBlank	: true
		}
		));

		this.HarvestDateBox = Ext.create("Ext.form.FieldSet",
				{
					title		: "Harvest Date",
					collapsible	: true,
					labelWidth	: 70,
					boxMinWidth	: 275,
					stateId		: 'harvest_date',
					stateful	: false,
					listeners	:
					{
						resize	:
						{
							fn	: function()
							{
								this.up('form').save_State();
							}
						}
					}
				});

		this.HarvestDateBox.add(Ext.create("Ext.form.field.Date",
		{
			fieldLabel	: "Start Date",
			dataType	: AmpleHarvest.objects.search.querypanel.form.AbstractSearchForm.C_STR_DATATYPE_DATE,
			anchor		: "100%",
			name		: this.self.C_STR_FIELD_HARVESTDATE + "_start",
			allowBlank	: true
		}
		));

		this.HarvestDateBox.add(Ext.create("Ext.form.field.Date",
		{
			fieldLabel	: "End Date",
			dataType	: AmpleHarvest.objects.search.querypanel.form.AbstractSearchForm.C_STR_DATATYPE_DATE,
			anchor		: "100%",
			name		: this.self.C_STR_FIELD_HARVESTDATE + "_end",
			allowBlank	: true
		}
		));

		this.IndexDateBox = Ext.create("Ext.form.FieldSet",
				{
					title		: "Index Date",
					collapsible	: true,
					labelWidth	: 70,
					boxMinWidth	: 275,
					stateId		: 'index_date',
					stateful	: false,
					listeners	:
					{
						resize	:
						{
							fn	: function()
							{
								this.up('form').save_State();
							}
						}
					}
				});

		this.IndexDateBox.add(Ext.create("Ext.form.field.Date",
		{
			fieldLabel	: "Start Date",
			dataType	: AmpleHarvest.objects.search.querypanel.form.AbstractSearchForm.C_STR_DATATYPE_DATE,
			anchor		: "100%",
			name		: this.self.C_STR_FIELD_INDEXDATE + "_start",
			allowBlank	: true
		}
		));

		this.IndexDateBox.add(Ext.create("Ext.form.field.Date",
		{
			fieldLabel	: "End Date",
			dataType	: AmpleHarvest.objects.search.querypanel.form.AbstractSearchForm.C_STR_DATATYPE_DATE,
			anchor		: "100%",
			name		: this.self.C_STR_FIELD_INDEXDATE + "_end",
			allowBlank	: true
		}
		));

		this.CostBox = Ext.create("Ext.form.FieldSet",
				{
					title		: "Cost",
					collapsible	: true,
					labelWidth	: 70,
					boxMinWidth	: 275,
					stateId		: 'cost',
					stateful	: false,
					listeners	:
					{
						resize	:
						{
							fn	: function()
							{
								this.up('form').save_State();
							}
						}
					}
				});

		this.CostBox.add(Ext.create("Ext.form.field.Number",
		{
			fieldLabel	: "Minimum",
			dataType	: AmpleHarvest.objects.search.querypanel.form.AbstractSearchForm.C_STR_DATATYPE_VALUE,
			anchor		: "100%",
			name		: this.self.C_STR_FIELD_COST + "_start",
			allowBlank	: true
		}
		));

		this.CostBox.add(Ext.create("Ext.form.field.Number",
		{
			fieldLabel	: "Maximum",
			dataType	: AmpleHarvest.objects.search.querypanel.form.AbstractSearchForm.C_STR_DATATYPE_VALUE,
			anchor		: "100%",
			name		: this.self.C_STR_FIELD_COST + "_end",
			allowBlank	: true
		}
		));

		this.m_obj_VKeyboard = Ext.create("AmpleHarvest.addOns.keyboard.cPlugin");

		this.ReadStatus = Ext.create('Ext.form.ComboBox',
				{
					fieldLabel		: 'Read',
					store			: Ext.create('Ext.data.Store',
							{
								fields	:
								[
										'label', 'term'
								],
								data	:
								[
										{
											"label"	: "Read",
											"term"	: "read"
										},
										{
											"label"	: "Unread",
											"term"	: "unread"
										},
										{
											"label"	: "All",
											"term"	: ""
										}
								// ...
								]
							}),
					anchor			: "100%",
					name			: "read",
					value			: "",
					queryMode		: 'local',
					displayField	: 'label',
					valueField		: 'term'
				});

		this.items =
		[
				Ext.create("Ext.form.field.Text",
				{
					fieldLabel		: "General",
					itemId			: 'f_general',
					name			: "general",
					hiddenName		: "general",
					inputType		: "textfield",
					allowBlank		: true,
					// value : g_arr_Requests["general"],
					anchor			: "92%",
					keyboardConfig	:
					{
						languageSelection	: true,
						language			: AmpleHarvest.addOns.keyboard.layout.Cyrillic_RU.C_STR_NAME,
						showIcon			: true
					},
					plugins			:
					[
						this.m_obj_VKeyboard
					]
				}
				),
				Ext.create("AmpleHarvest.elements.combobox.cFacet",
				{
					fieldLabel		: "Customer",
					name			: this.self.C_STR_FIELD_CUSTOMER,
					disabled		: false,
					m_str_Facet		: this.self.C_STR_FIELD_CUSTOMER,
					m_str_SolrType	: "reaper",
					displayField	: 'term',
					hiddenName		: this.self.C_STR_FIELD_CUSTOMER,
					valueField		: 'term',
					anchor			: "92%",
					emptyText		: 'Enter Customer',
					keyboardConfig	:
					{
						languageSelection	: true,
						language			: AmpleHarvest.addOns.keyboard.layout.Cyrillic_RU.C_STR_NAME,
						showIcon			: true
					},
					plugins			:
					[
						this.m_obj_VKeyboard
					]
				}
				),
				Ext.create("AmpleHarvest.elements.combobox.cFacet",
				{
					fieldLabel		: "Source",
					name			: this.self.C_STR_FIELD_SOURCE,
					disabled		: false,
					m_str_Facet		: this.self.C_STR_FIELD_SOURCE,
					m_str_SolrType	: "reaper",
					displayField	: 'term',
					hiddenName		: this.self.C_STR_FIELD_SOURCE,
					valueField		: 'term',
					emptyText		: 'Enter Source',
					anchor			: "92%",
					keyboardConfig	:
					{
						languageSelection	: true,
						language			: AmpleHarvest.addOns.keyboard.layout.Cyrillic_RU.C_STR_NAME,
						showIcon			: true
					},
					plugins			:
					[
						this.m_obj_VKeyboard
					]
				}
				),
				Ext.create("AmpleHarvest.elements.combobox.cFacet",
				{
					fieldLabel		: "Tax Payer ID",
					name			: this.self.C_STR_FIELD_TAXID,
					m_str_Facet		: this.self.C_STR_FIELD_TAXID,
					m_str_Prefix	: "hastaxpayerid",
					displayField	: 'term',
					hiddenName		: this.self.C_STR_FIELD_TAXID,
					valueField		: 'term',
					emptyText		: 'Enter Tax Payer ID',
					anchor			: "92%",
					keyboardConfig	:
					{
						languageSelection	: true,
						language			: AmpleHarvest.addOns.keyboard.layout.Cyrillic_RU.C_STR_NAME,
						showIcon			: true
					},
					plugins			:
					[
						this.m_obj_VKeyboard
					]
				}
				),

				Ext.create("AmpleHarvest.elements.combobox.cFacet",
				{
					fieldLabel		: "Order Method",
					name			: this.self.C_STR_FIELD_ORDERMETHOD,
					disabled		: false,
					m_str_Facet		: this.self.C_STR_FIELD_ORDERMETHOD,
					m_str_SolrType	: "reaper",
					displayField	: 'term',
					hiddenName		: this.self.C_STR_FIELD_ORDERMETHOD,
					valueField		: 'term',
					emptyText		: 'Enter Order Method',
					anchor			: "92%",
					keyboardConfig	:
					{
						languageSelection	: true,
						language			: AmpleHarvest.addOns.keyboard.layout.Cyrillic_RU.C_STR_NAME,
						showIcon			: true
					},
					plugins			:
					[
						this.m_obj_VKeyboard
					]
				}
				), this.ReadStatus, this.CostBox, this.PublishDateBox,
				this.HarvestDateBox, this.IndexDateBox
		]
	},

	load_FromJson		: function(obj_Json)
	{
		for (var int_I = 0; int_I < obj_Json.Query.length; int_I++)
		{
			if (obj_Json.Query[int_I].property == AmpleHarvestPHP.objects.search.querypanel.form.cGroundEffect.C_STR_FIELD_PUBLISHDATE)
			{
				if (obj_Json.Query[int_I].value)
				{
					this
							.set_FieldValue(
									AmpleHarvestPHP.objects.search.querypanel.form.cGroundEffect.C_STR_FIELD_PUBLISHDATE
											+ "_start",
									new Date(obj_Json.Query[int_I].value)
							);
				}

				if (obj_Json.Query[int_I].value_end)
				{
					this
							.set_FieldValue(
									AmpleHarvestPHP.objects.search.querypanel.form.cGroundEffect.C_STR_FIELD_PUBLISHDATE
											+ "_end",
									new Date(obj_Json.Query[int_I].value_end)
							);
				}
			}
			else if (obj_Json.Query[int_I].property == "<?php print(cDoc::C_STR_FIELD_HARVEST_DATE); ?>")
			{
				if (obj_Json.Query[int_I].value)
				{
					this
							.set_FieldValue(
									"<?php print(cDoc::C_STR_FIELD_HARVEST_DATE); ?>_start",
									new Date(obj_Json.Query[int_I].value)
							);
				}
				if (obj_Json.Query[int_I].value_end)
				{
					this
							.set_FieldValue(
									"<?php print(cDoc::C_STR_FIELD_HARVEST_DATE); ?>_end",
									new Date(obj_Json.Query[int_I].value_end)
							);
				}
			}
			else if (obj_Json.Query[int_I].property == "<?php print(cDoc::C_STR_FIELD_INDEX_DATE); ?>")
			{
				if (obj_Json.Query[int_I].value)
				{
					this
							.set_FieldValue(
									"<?php print(cDoc::C_STR_FIELD_INDEX_DATE); ?>_start",
									new Date(obj_Json.Query[int_I].value)
							);
				}
				if (obj_Json.Query[int_I].value_end)
				{
					this
							.set_FieldValue(
									"<?php print(cDoc::C_STR_FIELD_INDEX_DATE); ?>_end",
									new Date(obj_Json.Query[int_I].value_end)
							);
				}
			}
			else if (obj_Json.Query[int_I].property == AmpleHarvestPHP.objects.search.querypanel.form.cGroundEffect.C_STR_FIELD_COST)
			{
				if (obj_Json.Query[int_I].value)
				{
					this
							.set_FieldValue(
									AmpleHarvestPHP.objects.search.querypanel.form.cGroundEffect.C_STR_FIELD_COST
											+ "_start",
									obj_Json.Query[int_I].value
							);
				}
				if (obj_Json.Query[int_I].value_end)
				{
					this
							.set_FieldValue(
									AmpleHarvestPHP.objects.search.querypanel.form.cGroundEffect.C_STR_FIELD_COST
											+ "_end",
									obj_Json.Query[int_I].value_end
							);
				}
			}
			else if (obj_Json.Query[int_I].property == "read")
			{
				if (obj_Json.Query[int_I].condition == "!=")
				{
					this.set_FieldValue("read", "unread");
				}
				else
				{
					this.set_FieldValue("read", "read");
				}

			}
			else
			{
				var str_name = obj_Json.Query[int_I].property;
				if (obj_Json.Query[int_I].prefix)
				{
					str_name += "_"
							+ obj_Json.Query[int_I].prefix.substr(0,
									obj_Json.Query[int_I].prefix.length - 1
							);
				}

				this.set_FieldValue(str_name, obj_Json.Query[int_I].value);
			}
		}
	}

}

);